package com.wangsir.util;

import java.sql.*;

/*
 *1.作用是数据库配置信息
 *2.提供基本的和数据库交互的方法
 */
public class JdbcHelper {
    //加载地址
    private static final String ClassName = "com.mysql.cj.jdbc.Driver";
    //url地址
    private static final String url="jdbc:mysql://localhost:3306/remember?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";
    //数据库用户名
    private static final String user = "root";
    //数据库密码
    private static final String password = "123456";
    //数据库连接对象
    private Connection connection = null;
    //执行sql对象
    private PreparedStatement pstmt = null;
    //返回结果集对象
    private ResultSet rs = null;

    //测试
    public static void main(String[] args) throws SQLException {
        JdbcHelper jdbcHelper = new JdbcHelper();
        ResultSet resultSet = jdbcHelper.executeQuery("select * from tb_student");
        while(resultSet.next()){
            System.out.println(resultSet.getString("sno"));
            System.out.println(resultSet.getString("name"));
            System.out.println(resultSet.getInt("age"));
        }
    }

    //记载驱动，注册声名，当加载类的时候就加载这个静态代码块
    static{
        try {
            Class.forName(ClassName);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    //当创建这个类的对象的时候就会和数据库建立连接
    public JdbcHelper() {
        try {
            connection = DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //执行的是sql语句查询：返回的是结果集
    public ResultSet executeQuery(String sql, Object... params){
        try {
            //预处理这个sql语句
            pstmt = connection.prepareStatement(sql);
            //判断参数是否为空
            if(params != null){
                for(int i = 0; i < params.length; i++){
                    pstmt.setObject(i + 1,params[i]);
                }
            }
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return rs;
    }

    //执行的是数据库的更新，返回的是执行sql所影响的行数
    public  int executeUpdate(String sql, Object... params){
        int row = -1;
        try {
            pstmt = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1,params[i]);
            }
            row = pstmt.executeUpdate();//sql执行以后影响的行数
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return row;
    }
    //关闭开启的所有资源
    public void closeDB(){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(pstmt != null){
            try {
                pstmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
